# -*- coding: utf-8 -*-
import os
import time
from threading import Thread


class ThreadHelper(object):

    @staticmethod
    def thread_clean_workspace(root):
        """
        启动多个maven工作空间的清楚任务
        :param root:
        :return:
        """
        print('thread root', root)
        project_list = os.listdir(root)
        projects = [_ for _ in project_list if not _.startswith('.')]
        for p in projects:
            workspace = os.path.join(root, p)
            print('submit a workspace clean', workspace)
            Thread(target=ThreadHelper.__clean_maven_workspace, args=(workspace,)).start()


    @staticmethod
    def __clean_maven_workspace(workspace):
        """
        清空maven项目的工作空间
        :param workspace:
        :return:
        """
        work_path = os.listdir(workspace)
        if 'pom.xml' not in work_path:
            print('the workspace not a maven space', workspace)
            return
        print('=' * 20, 'Start Clean Space [%s]' % workspace, '=' * 20)
        starttime = time.time()
        os.chdir(workspace)
        os.system('mvn clean')
        endtime = time.time()
        print('=' * 20, 'Completed Clean Space [%s], Cost [%f]s' % (workspace, endtime - starttime), '=' * 20)
